﻿using System;
using System.Collections.Generic;

using Matchingo.Common.Constants;
using Matchingo.Common.Types.Interfaces;

namespace Matchingo.Models.CardProvider
{
    public class PathCardProvider : ICardSetProvider
    {
        #region ========== Private Properties ==========
        
        private IGameSettingsProvider SettingsProvider { get; set; }
		private List<ICardSetResources> CardSetResources { get; set; }

        #endregion

        #region ========== Public Properties ==========

        public List<string> CardSetsAvailable 
		{
			get { return GetCardSetNameList(); }
		}

        public List<Uri> CardImages 
		{
			get { return GetCardSetImages(); }
		}        

        #endregion

        #region ========== Constructors ==========

        public PathCardProvider(IGameSettingsProvider settingsProvider, List<ICardSetResources> cardSetResources)
        {
			SettingsProvider = settingsProvider;
            CardSetResources = cardSetResources;            
        }

        #endregion

        #region ========== Private Methods ==========

		private List<string> GetCardSetNameList()
		{
			List<string> cardSetNames = new List<string>();

			foreach (ICardSetResources cardResources in CardSetResources)
			{
				cardSetNames.AddRange(cardResources.GetCardSetNames());
			}

			return cardSetNames;
		}

		private List<Uri> GetCardSetImages()
		{			
			// if we are to get random cards then return all the sets combined ..
			if (SettingsProvider.GameSettings.CardSetName == CommonConstants.GAME_SETTINGS_IMAGESETS_RANDOM)
			{
				List<Uri> randomCardSetImages = new List<Uri>();

				// go through all the card providers (plugins) and find all the sets they provide, and add
				// them all to our list of card images
				foreach (ICardSetResources cardResources in CardSetResources)
				{
					Dictionary<string, List<Uri>> dict = cardResources.GetCardSetsProvided();
					foreach (string key in dict.Keys)
					{
						randomCardSetImages.AddRange(dict[key]);
					}
				}

				return randomCardSetImages;
			}

		    List<Uri> cardSetImages = GetCardResourcesFromName(CardSetResources, SettingsProvider.GameSettings.CardSetName);

			// we werent able to find the cardset set in the users options.. this might happen if that cardset is no longer available
			// in which case this should already have been handled elsewhere, so we will throw an exception
			if (cardSetImages == null)
			{
				// forcefully set their settings back to random ..
				SettingsProvider.GameSettings.CardSetName = CommonConstants.GAME_SETTINGS_IMAGESETS_RANDOM;
				SettingsProvider.SaveSettings();

				// .. and call ourself to get a random set
				return GetCardSetImages();
			}	

			return cardSetImages;
		}

		private static List<Uri> GetCardResourcesFromName(IEnumerable<ICardSetResources> cardResourcesList, string name)
		{
			foreach (ICardSetResources cardResources in cardResourcesList)
			{
				if (cardResources.GetCardSetNames().Contains(name))
				{
					return cardResources.GetCardSetsProvided()[name];
				}
			}

			return null;
		}

        #endregion             
    }
}
